# In this example we will have a QLineEdit item and a QButton item. We will drag text from the QLineEdit widget and drop
# it on the QButton widget.

import sys
from PySide import QtGui, QtCore

# In order to drop text on the QtGui.QPushButton widget, we must re-implement some methods. So we create our own Button
# class, which will inherit from the QtGui.QPushButton class.
class Button(QtGui.QPushButton):
    def __init__(self, title, parent):
        super(Button, self).__init__(title, parent)
        self.setAcceptDrops(True)   # Enable Drops for this Widget

    # This event handler is called when a drag is in progress and the mouse enters this widget. The event is passed
    # in the event parameter. If the event is ignored, the widget won’t receive any drag move events.
    # First we re-implement the dragEnterEvent() method. We inform about the data type, we will accept.
    # In our case it is plain text.
    def dragEnterEvent(self, event):
        # QtCore.QMimeData.hasFormat(mimetype), returns 'true' if the object can return data for the MIME type
        # specified by mimeType, otherwise returns 'false'. For the most common types of data, you can call the
        # higher-level functions PySide.QtCore.QMimeData.hasText(), hasHtml(), hasUrls(), hasImage(), hasColor() etc.
        if event.mimeData().hasFormat('text/plain'):
            event.accept()
        else:
            event.ignore()
    # This event handler is called when the drag is dropped on this widget. The event is passed in the event parameter.
    # First we re-implement the dragEnterEvent() method. We inform about the data type, we will accept.
    # In our case it is plain text.
    def dropEvent(self, event):
        self.setText(event.mimeData().text())


class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()

        self.setGeometry(200, 200, 300, 200)
        self.setWindowTitle("Drag and Drop")

        # The QtGui.QLineEdit widget has a built-in support for drag operations. All we need to do is to call
        # setDragEnabled() method to activate it.
        lineEdit = QtGui.QLineEdit("", self)
        lineEdit.setDragEnabled(True)   # Enable Drops for this Widget
        lineEdit.move(30, 65)

        btn = Button("Button", self)
        btn.move(190, 65)

        label = QtGui.QLabel("Drag text from the line edit box on the button", self)
        label.move(10, 10)


        self.show()


if __name__ == "__main__":
    appObject = QtGui.QApplication(sys.argv)
    ex = Window()
    ex.show()
    sys.exit(appObject.exec_())
